<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
use app\common\model\Homework as HomeworkModel;
use app\common\model\HomeworkAssignmentRelation;

/**
 * 作业列表接口
 */
class Homework extends Api
{

    /**
     * 作业列表
     * @var array
     */
    protected $noNeedRight = '*';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new HomeworkModel();
        $this->homeworkAssignmentRelation = new HomeworkAssignmentRelation();
    }

    /**
     * 查询作业列表
     * @ApiTitle    (作业列表)
     * @ApiMethod  (GET)
     * @ApiParams (name="student_id", type="int", required=true, description="学生ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
        'code':'200',
        'msg':'查询成功',
        'time': "",
        'data': $row
      })
     */
    public function homeworkIndex()
    {
        $data = input('get.');
        $studentId = $data['student_id'];
        if (!$studentId || !Validate::is($studentId, 'number')) {
            $this->error('学生ID错误');
        }

        $dis = [
            'student_id' => $studentId
        ];
        // 获取作业数据
        $row = $this->model->dataList($dis);
        $this->success('查询成功', $row);
    }
        
    /**
     * 查询布置作业完成的作业列表
     * @ApiTitle    (作业列表)
     * @ApiMethod  (GET)
     * @ApiParams (name="student_id", type="int", required=true, description="学生ID")
     * @ApiParams (name="assignment_id", type="int", required=true, description="布置作业ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     * @ApiReturn   ({
        'code':'200',
        'msg':'查询成功',
        'time': "",
        'data': $row
      })
     */
    public function homeworkAssignment()
    {
        $data = input('get.');
        $studentId = $data['student_id'];
        $assignmentId = $data['assignment_id'];
        if(!$studentId || !Validate::is($studentId, 'number') || !$assignmentId || !Validate::is($assignmentId, 'number')){
            $this->error('学生ID或布置作业ID错误');
        }
        $dis = [
            'assignment_id' => $assignmentId,
        ];
        $row = $this->homeworkAssignmentRelation->dataList($dis);
        $homeworkIds = array_column($row, 'homework_id');

        $dis = [
            'id' => ['in', $homeworkIds],
            'student_id' => $studentId
        ];
        $row = $this->model->dataList($dis);

        $this->success('查询成功', $row);
    
    }


}